﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Prism.Commands;
using Prism.Interfaces;
using System.Windows;
using EmployeeModule.EmployeeChooser;
using System.Diagnostics;
using Domain;
using System.ComponentModel;
using System.Windows.Input;

namespace EmployeeModule.EmployeeDetail
{
    public static class EmployeeDetailCommands
    {
        public static RoutedCommand SaveEmployee = new RoutedCommand();
        public static RoutedCommand ReloadEmployee = new RoutedCommand();
    }

    public class EmployeeDetailController: IEmployeeDetailController
    {
        private IUnityContainer container;
        private IEmployeeDetailView view;

        EmployeeDetailViewModel model;


        public EmployeeDetailController(IUnityContainer container, IEmployeeDetailView view)
        {
            this.container = container;
            this.view = view;


            ((UIElement) this.view).CommandBindings.Add(
                new CommandBinding(EmployeeChooserCommands.ChooseEmployee,
                    Choose));

            ((UIElement) this.view).CommandBindings.Add(
                new CommandBinding(EmployeeDetailCommands.SaveEmployee,
                    Save));

            ((UIElement) this.view).CommandBindings.Add(
    new CommandBinding(EmployeeDetailCommands.ReloadEmployee,
        Reload));

            model = new EmployeeDetailViewModel();
        }


        bool isAdded = false;
        public void Show(IRegion region, Employee employee)
        {
            // we want to show ourself
            if (!isAdded)   // region is not yet implemented fully. so workarounds
            {
                region.Add((UIElement) view);
                view.SetModel(model);
                isAdded = true;
            }
            model.DetailedEmployee = employee;
        }

        public void Choose(object sender, RoutedEventArgs e)
        {
            IEmployeeChooserController chooser = container.Resolve<IEmployeeChooserController>();
            chooser.Show(delegate(Employee chosen)
            {
                // remove teammember from teamleader
                if (model.DetailedEmployee.TeamLeader != null)
                {
                    model.DetailedEmployee.TeamLeader.TeamMembers.Remove(model.DetailedEmployee);
                }

                // add from both sides
                model.DetailedEmployee.TeamLeader = chosen;
                model.DetailedEmployee.TeamLeader.TeamMembers.Add(model.DetailedEmployee);

            });
        }

        public void Save(object sender, RoutedEventArgs e)
        {
            EmployeeService svc = new EmployeeService();
            model.DetailedEmployee = (Employee) svc.SavePerson(model.DetailedEmployee);
        }

        public void Reload(object sender, RoutedEventArgs e)
        {
            EmployeeService svc = new EmployeeService();
            model.DetailedEmployee = svc.GetEmployee(model.DetailedEmployee.PersonID);
        }

        public class EmployeeDetailViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged = delegate { };

            Employee detailedEmployee;

            public Employee DetailedEmployee
            {
                get { return detailedEmployee; }
                set
                {
                    detailedEmployee = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("DetailedEmployee"));
                }
            }
        }

    }
}
